<?php
/**
 * Created by PhpStorm.
 * User: Winter
 * Date: 2016/5/1
 * Time: 10:54
 */
namespace frontend\controllers;
use Yii;
use yii\web\Response;
use frontend\models\Illegal;

class QueryController extends BaseController {

    /**
     * 打开查询页
     *
     * @return string
     */
     public function actionIndex() {
         Yii::info('query:::::index','error');
         return $this->render('index');
     }

     public function actionResult() {
         return $this->render('result');
     }

    /**
     * 查询
     *
     * @return string
     */
    public function actionGetQueryDetails() {
        Yii::info('actionGetQueryDetails','error');
//        sleep(2);//模拟网络，等待3秒后返回
        $request = Yii::$app->request;

        $response = Yii::$app->response;
        $response->format = Response::FORMAT_JSON;

        $carNo = $request->get('carNo');
        Yii::info('carNo====', $carNo);
        $bodyNo = $request->get('bodyNo');
        Yii::info('bodyNo====', $bodyNo);

        if (($checkresult = $this->checkCarNo($carNo)) !== true) {
            $result = ['code' => 1, 'error' => $checkresult];
            $response->data = $result;
            Yii::info('carNo====', 'error');
            return;
        }

        if (($checkresult = $this->checkBodyNo($bodyNo)) !== true) {
            $result = ['code' => 2, 'error' => $checkresult];
            $response->data = $result;
            Yii::info('bodyNo====', 'error');
            return;
        }
        $result = Illegal::getList($carNo,$bodyNo);
        return $result;
    }

    private function checkCarNo($carNo) {
        if (!$carNo || strlen(trim($carNo)) == 0) {
            return "车牌号码不能为空";
        } else {
            $reg = '/^[\x4e00-\x9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/';
            if(preg_match($reg, $carNo)) {
                return "请输入正确的车牌号码";
            }
        }
        return true;
    }

    private function checkBodyNo($bodyNo) {
        if (!$bodyNo || strlen(trim($bodyNo)) == 0) {
            return "车身架号不能为空";
        }
        return true;
    }

    public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'maxLength' => 4, //最大显示个数
                'minLength' => 4,//最少显示个数
                'padding' => 6,//间距
                'height'=>40,//高度
                'width' => 80,  //宽度
                'offset'=>4,//设置字符偏移量 有效果
            ],
        ];
    }
}
